from django.contrib import messages
from django.http import HttpResponseRedirect
from django.shortcuts import render, get_object_or_404, redirect

# Create your views here.
from django.urls import reverse

from blog.models import Article
from comments import models
from comments.forms import CommentForm
from comments.models import Comment


def comment(request, sid):
    show = Article.objects.get(id=sid)  # 查询指定id的文章
    # print(show)
    if request.method == 'POST':
        comm_name = request.POST.get('comm_name', None)
        comm_email = request.POST.get('comm_email', None)
        text = request.POST.get('text', None)
        if not comm_name:
            err_message = '请输入内容'
            comm_count = show.comment_set.count()
            comment_list = show.comment_set.all()
            return render(request, 'show.html', locals())
            # messages.success(request, "请输入内容")
        else:
            print(comm_name, comm_email, text, sid)
            user_comment = models.Comment()
            user_comment.name = comm_name
            user_comment.email = comm_email
            user_comment.text = text
            user_comment.post = show
            user_comment.save()
            message = '评论成功'
            comm_count = show.comment_set.count()
            comment_list = show.comment_set.all()
            # messages.success(request, "评论成功")
            # return redirect(show)
    return render(request, 'show.html', locals())
